package com.wmyg.antifake.config;

import com.wmyg.antifake.oauth.SysUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.io.UnsupportedEncodingException;
import java.security.SecureRandom;

/**
 * @program 前后端分离版妙码后台
 * @description api安全控制配置
 * @author 王卓逸
 * @create 2019-04-15
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    /**
     * 新建密码加密编码器
     * @return bcrypt编码器
     * @throws UnsupportedEncodingException 防止默认字符集不同，此异常可忽略
     */
    @Bean
    public PasswordEncoder passwordEncoder() throws UnsupportedEncodingException {
        return new BCryptPasswordEncoder(10, new SecureRandom("Wmyg2019".getBytes("UTF-8")));
//        return NoOpPasswordEncoder.getInstance();
    }

    /**
     * 自动注入密码加密编码器
     */
//    @Autowired
//    PasswordEncoder passwordEncoder;

    @Autowired
    SysUserDetailService sysUserDetailService;

    /**
     * 配置认证管理器
     * @param auth 认证管理配置类
     * @throws Exception 可忽略
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(sysUserDetailService)
                .passwordEncoder(passwordEncoder());
//        auth.inMemoryAuthentication()
//                .withUser("wwwwww")
//                .password(passwordEncoder.encode("123456"))
//                .roles("USER");
    }

    /**
     * 安全配置
     * @param http 请求安全拦截配置类
     * @throws Exception 可忽略
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestMatchers().anyRequest()
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.OPTIONS, "/oauth/**", "/logout", "/sign/**", "/notice/**", "/picture/**", "/record/downloadFile",
                        "/v2/api-docs", "/swagger-resources/configuration/ui",
                        "/swagger-resources","/swagger-resources/configuration/security",
                        "/swagger-ui.html","/css/**", "/js/**","/images/**", "/webjars/**", "**/favicon.ico").permitAll()
                .and().cors();
    }

    /**
     * password模式必须有的
     * @return 认证管理器
     * @throws Exception 可忽略
     */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}
